package com.xunyin.modules.cleaningrecord.rest;

import com.github.pagehelper.util.StringUtil;
import com.xunyin.constant.Constants;
import com.xunyin.modules.log.domain.TCleaningRecord;
import com.xunyin.modules.log.service.TCleaningRecordService;
import com.xunyin.modules.security.security.TokenUtil;
import com.xunyin.modules.security.security.vo.JwtUser;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 保洁管理controller
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@AllArgsConstructor
@Api(tags = "共享空间:保洁管理")
@RestController
@RequestMapping("api/cleaning")
public class BackendCleaningRecordController {

    private final TCleaningRecordService tCleaningRecordService;
    private final TokenUtil tokenUtil;

    /**
     * 余额明细列表查询
     *
     * @param request HttpServletRequest对象
     * @return 余额明细列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> list(HttpServletRequest request) {

        Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
        String mobile = request.getParameter("mobile") == null ? "" : request.getParameter("mobile");
/*        String userId = request.getParameter("userId") == null ? "" : request.getParameter("userId");
        String orderSn = request.getParameter("orderSn") == null ? "" : request.getParameter("orderSn");
        String status = request.getParameter("status") == null ? StatusEnum.ENABLED.getKey() : request.getParameter("status");*/
        Integer storeId = (request.getParameter("storeId") == null || StringUtil.isEmpty(request.getParameter("storeId"))) ? 0 : Integer.parseInt(request.getParameter("storeId"));

        Map<String, Object> searchParams = new HashMap<>();
        if (StringUtil.isNotEmpty(mobile)) {
            searchParams.put("cleannerPhone", mobile);
        }
 /*        if (StringUtil.isNotEmpty(userId)) {
            searchParams.put("userId", userId);
        }
        if (StringUtil.isNotEmpty(orderSn)) {
            searchParams.put("orderSn", orderSn);
        }
        if (StringUtil.isNotEmpty(status)) {
            searchParams.put("status", status);
        }*/
//        if (storeId > 0) {
//            searchParams.put("storeId", storeId);
//        }
        JwtUser user = tokenUtil.getJwtUser(tokenUtil.getToken(request));
        if (!user.getRoleIdList().contains(Long.valueOf(1L))) {
            searchParams.put("storeIdList", user.getStoreIdList());
        }
        PaginationRequest paginationRequest = new PaginationRequest();
        paginationRequest.setCurrentPage(page);
        paginationRequest.setPageSize(pageSize);
        paginationRequest.setSearchParams(searchParams);

        PaginationResponse<TCleaningRecord> paginationResponse = tCleaningRecordService.queryCleaningRecordListByPagination(paginationRequest);

        Map<String, Object> result = new HashMap<>();
        result.put("paginationResponse", paginationResponse);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}
